<?php

Class RoleModel
{
    public function __construct()
    {
        $this->db = Yaf_Registry::get('_db');
    }

    public function find($id)
    {
        $sql = "select * from t_role where id = $id";
        $result = $this->db->get_row($sql);
        if ($result){
            return $result;
        }else {
            return NULL;
        }
    }

    public function selectAll($start = 0, $length = 20, $searchkey = '')
    {
        $wheresql = ' where 1=1 ';
        if (isset($searchkey) && !empty($searchkey)){
            $wheresql .= " and name like '%$searchkey%'";
        }
        
        // 获取总数
        $countsql = "select count(id) from t_role " . $wheresql;
        $total = $this->db->get_var($countsql);
        
        // 获取分页数据
        $sql = "select id, name, description, status 
                    from t_role
                    " . $wheresql . " order by id desc limit $start, $length";

        $result = $this->db->get_results($sql);

        return array($total, $result);
    }
    
    // 对用户数据进行新增或修改时提供role数据进行选择
    public function getroledata()
    {
        $sql = "select id, name
                    from t_role
                    where status = 1 
                    order by id desc";
        $powers = $this->db->get_results($sql);
        return $powers;
    }
    
    public function getaccess($id)
    {
        $sql = "select powerid  
                    from t_role_access
                    where roleid = $id and status = 1";
        $access = $this->db->get_col($sql);
        return $access;
    }
    
    public function getaccess_toplevel($roleid)
    {
        if ($roleid == 1){
            $sql = "select 1 as view, title, concat('/admin/', controller, '/', action) as url, id as powerid
                        from t_power
                        where status = 1 and parentid = 0 and (type = 1 or type =3)";
        }else{
            $sql = "select 1 as view, b.title, concat('/admin/', b.controller, '/', b.action) as url, a.powerid
                        from t_role_access as a
                        inner join t_power as b on a.powerid = b.id 
                        where a.roleid = $roleid and a.status = 1 and b.parentid = 0 and (b.type = 1 or b.type =3)";
        }
        $access = $this->db->get_results($sql);
        return $access;
    }
    
    public function getaccess_sub($roleid, $parentid)
    {
        if ($roleid == 1){
            $sql = "select 1 as view, title, concat('/admin/', controller, '/', action) as url, id as powerid
                        from t_power
                        where status = 1 and parentid = $parentid and (type = 1 or type =3)";
        }else{
            $sql = "select 1 as view, b.title, concat('/admin/', b.controller, '/', b.action) as url, a.powerid
                        from t_role_access as a
                        inner join t_power as b on a.powerid = b.id 
                        where a.roleid = $roleid and a.status = 1 and b.parentid = $parentid and (b.type = 1 or b.type =3)";
        }
        $access = $this->db->get_results($sql);
        return $access;
    }

    public function insert($info)
    {
        $name = $info['name'];
        $description = $info['description'];
        $createtime = date('Y-m-d H:i:s');
        
        $sql = "insert into t_role(name, description, createtime, status)
                    values('$name', '$description', '$createtime', 1)";
                    
        $result = $this->db->query($sql);
        return $result;
    }

    public function update($info)
    {
        $name = $info['name'];
        $description = $info['description'];
        $status = $info['status'];
        $id = $info['id'];
        
        $sql = "update t_role 
                    set name = '$name', 
                        description = '$description',
                        status = $status 
                    where id = $id";
        $result = $this->db->query($sql);
        return $result;
    }
    
    public function updateaccess($id, $access)
    {
        // 清理数据
        $clearsql = "delete from t_role_access where roleid = $id";
        $this->db->query($clearsql);
        
        if ($access){
            // 插入新的数据
            $createtime = date('Y-m-d H:i:s');
            $sql = "insert into t_role_access(roleid, powerid, createtime, status) values ";
            foreach ($access as $item) {
                $sql .= "($id, $item, '$createtime', 1),";
            }
            $sql = substr($sql, 0, strlen($sql) - 1);
            $result = $this->db->query($sql);
            return $result;
        }
    }
    
    public function checkaccess($uid, $controller, $action)
    {
        $sql = "select b.id 
                    from t_admin as a
                    inner join t_role_access as b on a.roleid = b.roleid
                    inner join t_power as c on b.powerid = c.id
                    where a.id = $uid and 
                        c.controller = '$controller' and 
                        c.action = '$action' and 
                        c.status = 1 and b.status = 1 and a.status = 1";
                        
        $result = $this->db->get_var($sql);
        return $result;
    }
}
